/*
Copyright (C) Ariff Ehsan, Abdul Hadi Fikri

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

/*  Author : Ariff Ehsan
 *  Date   : Jan 11, 2010
 */


#include <system.h>


void APP_CXBEE_startdone(error_t);
void APP_TIMER_run(void);
void APP_TIMERSTART_run(void);

timer_t app_timer, app_timerStart;

uint8_t app_startCount;


void APP_booted(){
	
	TIMER_reg(&app_timer, APP_TIMER_run);
	TIMER_reg(&app_timerStart, APP_TIMERSTART_run);
	
	TIMER_start(&app_timerStart, 100);
	
	LEDS4_init();
	
	C328R_init();
	C328R_reg(XCAMERA_startdone, XCAMERA_getdone, XCAMERA_recvpic);
	
	CXBEE_reg(APP_CXBEE_startdone, XPACKET_sendDone, XPAKCET_receive);
}


void APP_CXBEE_startdone(error_t err) {
	
	if (err != SUCCESS) {
		LEDS4_on(LEDS4_RED);
		return;
	}
	
	LEDS4_off(LEDS4_GREEN);
	
	XPACKET_init();
	XMOTOR_init();
	XCAMERA_init();
}


void APP_TIMER_run(void) {
}

void APP_TIMERSTART_run(void) {
	
	if (app_startCount++ < 10) {
		
		TIMER_start(&app_timerStart, 100);
		LEDS4_toggle(LEDS4_OREN);
	}
	else {
		
		if (CXBEE_start() == SUCCESS) {
			LEDS4_on(LEDS4_GREEN);
		}
	}
}
